package com.key.controller;


import com.key.common.R;
import com.key.common.vo.UserVO;
import com.key.entity.SysUser;
import com.key.service.SysUserService;
import com.key.utils.JwtUtils;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@Api(value = "登录退出",tags = "登录退出接口API")
public class LoginController {

    @Autowired
    SysUserService sysUserService;

    @RequestMapping(value = "/suLogin",method = RequestMethod.POST)
    @ApiOperation(value = "管理端登录接口",notes = "验证用户的用户名和密码",httpMethod = "post", response = R.class)
   /* @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "账户",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "password",value = "密码",paramType = "query",dataType = "String",required = true)
    })*/
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R adminLogin(@RequestBody UserVO userVO, HttpServletResponse response){
        System.out.println(userVO.getUserName());
        System.out.println(userVO.getPassword());
        return sysUserService.login(userVO,response);
    }

   /* @RequestMapping(value = "/userLogin",method = RequestMethod.POST)
    @ApiOperation(value = "用户端登录接口",notes = "验证用户的用户名和密码",httpMethod = "post", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "账户",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "password",value = "密码",paramType = "query",dataType = "String",required = true)
    })
    @ApiResponses({
            @ApiResponse(code=200,message="登录成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R userLogin(String userName, String password, HttpServletResponse response){

        return R.ok();
    }*/
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    @ApiOperation(value = "退出登录接口",notes = "退出登录",httpMethod = "get", response = R.class)
    @ApiResponses({
            @ApiResponse(code=200,message="退出成功"),
            @ApiResponse(code=401,message="token为空"),
            @ApiResponse(code=1005,message="Token过期"),
            @ApiResponse(code=1006,message="Token验证不通过"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R logout(){

        R r = new R();
        Subject subject = SecurityUtils.getSubject();
        subject.logout();//退出
        return R.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/refresh",method = RequestMethod.GET)
    public R refresh_token(HttpServletResponse response){
        Subject subject = SecurityUtils.getSubject();
        String principal = (String) subject.getPrincipal();
        System.out.println( principal);

        //生成jwt
        String token  = JwtUtils.createJwt(UUID.randomUUID().toString(),JwtUtils.JWT_SECERT,principal,JwtUtils.TTMILLIS);
        //refresh_token
        String refreshToken = JwtUtils.createJwt(UUID.randomUUID().toString(),JwtUtils.JWT_SECERT,principal,JwtUtils.RTTMILLIS);
        response.setHeader("Authorization",token);
        response.setHeader("RAuthorization",refreshToken);
        return R.ok();
    }
}
